﻿import com.embedchat.core.connection.ConnectionConstants;

class com.embedchat.core.connection.SocketConnectionClass extends XMLSocket{
	static var 	instanceName:String = new String ("SocketConnectionClass");
	static var instanceOwner:Object = com.embedchat.core.connection.SocketConnectionClass;
	private var isConnectionEstablished:Boolean = false;
	function SocketConnectionClass(){
		super();
		this.isConnectionEstablished = this.connect(ConnectionConstants.SERVER_ADRESS, ConnectionConstants.SERVER_PORT);
		if(!this.isConnectionEstablished){
			trace (SocketConnectionClass.instanceName+": Connection failed!")
		}
	}
	private function onConnect(success):Void{
		this.isConnectionEstablished = success;
		if(this.isConnectionEstablished){
			trace (SocketConnectionClass.instanceName+": Connection established!");			
		}else{
			trace (SocketConnectionClass.instanceName+": Connection failed!");
		}
	}
	
	public function sendData(msgObj:Object):Boolean{
		var stringForTransfer:String = null;
		switch(msgObj.msg){
			case ConnectionConstants.MSG_ID_TEXT_MESSAGE: //msg, body : {010006sample}
				stringForTransfer = this.encodeTextMessage(msgObj);
			break;
			case ConnectionConstants.MSG_ID_LOGIN: //msg,room,login,password {02002000000001:koba:123qwe}
				stringForTransfer = this.encodeLoginMessage(msgObj);			
			break;
			default:
				stringForTransfer = null;
			break;
		}
		trace(stringForTransfer);
		if(stringForTransfer && this.isConnectionEstablished){
			this.send(stringForTransfer);
			return true;
		}else{
			return false;
		}
		return false;
	}
	
	private function encodeTextMessage(msgObj:Object):String{
		var output:String = "";
		var outputLength:String = "";		
		var messageBodyBytesSize:String = msgObj.body.length.toString();
		
		if(msgObj.body.length <= 0 || msgObj.body.length > ConnectionConstants.MAX_MESSAGE_BODY_LENGTH){
			return null; //very long or empty body
		}
		outputLength = this.setLeaderZeros(msgObj.body.length.toString(),ConnectionConstants.MAX_MESSAGE_BODY_LENGTH.toString());
		trace(outputLength);		
		output = msgObj.msg+outputLength+msgObj.body;
		return output;
	}
	
	private function encodeLoginMessage(msgObj:Object):String{
		var output:String = "";
		var outputLength:String = "";
		var chatRoomID:Number = parseInt(msgObj.room);
		var messageBodyBytesSize:String = msgObj.body.length.toString();
		if((msgObj.login.length <= 0 || msgObj.login.length > ConnectionConstants.MAX_LOGIN_LENGTH)){
			return null; //very long or empty login
		}
		if((msgObj.password.length <= 0 || msgObj.password.length > ConnectionConstants.MAX_PASSWORD_LENGTH)){
			return null; //very long or empty password
		}
		if(chatRoomID == NaN || chatRoomID == null || chatRoomID < 0 || chatRoomID >ConnectionConstants.MAX_CHAT_ROOM_COUNT){
			return null; //invalid room id
		}
		msgObj.room = this.setLeaderZeros(msgObj.room,ConnectionConstants.MAX_CHAT_ROOM_COUNT.toString());
	
		output = msgObj.room+":"+msgObj.login+":"+msgObj.password;
		outputLength = output.length.toString();
		outputLength = this.setLeaderZeros(outputLength,ConnectionConstants.MAX_MESSAGE_BODY_LENGTH.toString())
		output = msgObj.msg+outputLength+output;
		return output;
	}
	
	private function setLeaderZeros(value:String, limiter:String):String{
		while(value.length < limiter.length){
			value = "0"+value;
		}
		return value;
	}
	
	/*public function onData(response:String):Void{
		trace("Yo");
		trace(response);
	}*/
	
	public function SocketConnectionClose():Void{
		if(this.isConnectionEstablished){
			this.close();
		}
	}
	
}